﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class PipeAlignment : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			if (commandData == null)
			{
				throw new NotImplementedException();
			}
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Document document = activeUIDocument.Document;
			Selection selection = activeUIDocument.Selection;
			List<ViewType> viewTypes = YJKRevitTools.ListViewNeed(2);
			if (!YJKRevitTools.ViewCheck(activeUIDocument, viewTypes))
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			FormPipeAlignMent formPipeAlignMent = null;
			Result result;
			try
			{
				RevitVersionFuncs.ClearSelection(selection);
				transaction = new Transaction(document, "水管对齐");
				transaction.Start();
				formPipeAlignMent = new FormPipeAlignMent();
				if (formPipeAlignMent.ShowDialog() == DialogResult.OK)
				{
					for (;;)
					{
						try
						{
							ISelectionFilter selectionFilter = new HengPipeSelectionFilter();
							ISelectionFilter selectionFilter2 = new RiserPipeSelectionFilter();
							ISelectionFilter selectionFilter3 = new PipeHengSelectionFilter();
							if (formPipeAlignMent.style == "横基准" && formPipeAlignMent.message != "参考线对齐")
							{
								Reference reference = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "选择基准水管");
								Pipe pipe = document.GetElement(reference) as Pipe;
								reference = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "选择对齐水管");
								Pipe pipe2 = document.GetElement(reference) as Pipe;
								if (!new HYPipeAlignment().PipeAlignment(pipe, pipe2, formPipeAlignMent.message))
								{
									MessageBox.Show("操作失败", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
								}
							}
							if (formPipeAlignMent.style == "横基准" && formPipeAlignMent.message == "参考线对齐")
							{
								new ElementContainsLocationCurveFilter();
								Reference reference = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "请选择基准管道，靠近鼠标点击位置一侧的边线为基准参考线");
								XYZ globalPoint = reference.GlobalPoint;
								Line pipingReferenceLine = new YJKPiping<Pipe>(document.GetElement(reference) as Pipe, globalPoint).getPipingReferenceLine();
								reference = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "选择要对齐的管道进行对齐，靠近鼠标点击位置的一侧边线为对齐边线");
								Pipe pipe3 = document.GetElement(reference) as Pipe;
								XYZ xyz = reference.GlobalPoint;
								Line pipingReferenceLine2 = new YJKPiping<Pipe>(pipe3, xyz).getPipingReferenceLine();
								YJKLine hyline = new YJKLine(pipingReferenceLine);
								xyz = pipingReferenceLine2.GetEndPoint(0);
								if (hyline.IsParallel(pipingReferenceLine2))
								{
									new HYPipeAlignment().PipeAlignmentReference(pipingReferenceLine, pipe3, xyz);
								}
								else
								{
									MessageBox.Show("参考线对齐，两边线必须平行才可以进行！", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
								}
							}
							if (formPipeAlignMent.style == "立基准")
							{
								Reference reference = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter2, "选择基准立管");
								Pipe pipe4 = document.GetElement(reference) as Pipe;
								if (formPipeAlignMent.message == "立对正")
								{
									reference = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter3, "选择对齐水管");
								}
								else
								{
									reference = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter2, "选择对齐水管");
								}
								Pipe pipe5 = document.GetElement(reference) as Pipe;
								if (!new HYPipeAlignment().PipeAlignment(pipe4, pipe5, formPipeAlignMent.message))
								{
									MessageBox.Show("操作失败", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
								}
							}
							continue;
						}
						catch (Autodesk.Revit.Exceptions.OperationCanceledException)
						{
						}
						break;
					}
				}
				transaction.Commit();
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				message = ex.Message;
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				if (transaction != null && 1 == (int)transaction.GetStatus())
				{
					transaction.RollBack();
				}
			}
			return result;
		}
	}
}
